package com.gitee.aurora.server.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.DateUtils;
import com.gitee.aurora.core.utils.ResultVO;
import com.gitee.aurora.core.utils.ResultVOUtils;
import com.gitee.aurora.server.service.ReportServive;
import com.gitee.aurora.server.vo.PtSjzxVo;
import com.gitee.aurora.server.vo.ReportExport;
import com.gitee.aurora.server.vo.ReportVo;
import com.gitee.aurora.utils.PageParam;
import com.gitee.aurora.utils.Pagination;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author gaoxu
 * 日志报表功能
 */
@Controller
@ResponseBody
@RequestMapping("report")
public class ReportController {
    @Autowired
    private ReportServive reportServive;

    /**
     *查询日志报表接口
     */
    @RequestMapping("/queryReport")
    @ApiOperation(value = "查询日志报表数据", notes = "查询日志报表数据")
    public ResultVO<Pagination> queryReport(@RequestBody ReportVo reportVo, PageParam pageParam){
        Pagination pagination = reportServive.queryReport(reportVo, pageParam);
        return ResultVOUtils.success(pagination);
    }

    /**
     *查询数据中心全量下拉列表
     */
    @RequestMapping("/querySjzxList")
    @ApiOperation(value = "查询数据中心下拉列表", notes = "查询数据中心下拉列表")
    public ResultVO<List<PtSjzxVo>> querySjzxList(){
        List<PtSjzxVo> ptSjzxVos=reportServive.querySjzxList();
        return ResultVOUtils.success(ptSjzxVos);
    }

    /**
     * 导出功能
     */
    @GetMapping("/reportExport")
    @ApiOperation(value = "导出功能", notes = "导出功能")
    public void reportExport(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ReportVo reportVo = new ReportVo();
        SimpleDateFormat formatter = new SimpleDateFormat(DateUtils.DATE_FORMAT_14);
        reportVo.setSourceIp(request.getParameter("sourceIp"));
        reportVo.setAbnormalFlag(request.getParameter("abnormalFlag"));
        reportVo.setStartTime(request.getParameter("startTime"));
        reportVo.setEndTime(request.getParameter("endTime"));
        List<ReportExport> reportExportList = reportServive.reportExport(reportVo);
        String fileName = "核查日志报表"+ formatter.format(new Date());
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ReportExport.class).sheet(formatter.format(new Date())).doWrite(reportExportList);
    }


}
